package com.letv.app.ljodfh.ui.utils;

import android.content.Context;
import android.support.annotation.NonNull;

import com.letv.app.ljodfh.ui.adapter.LiveHomeAdapter;
import com.letv.app.ljodfh.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/7.
 */

public class LiveHomeDataAddHelper {

    private LiveHomeAdapter adapter;

    private Object syncObj;

    private int advancePos = -1, livingTitlePos = -1, retrospectPos = -1;

    public LiveHomeDataAddHelper() {
        syncObj = new Object();
    }

    private void initAdapter(Context context, List<IListShowData> data) {
        adapter = new LiveHomeAdapter(context, data);
        advancePos = -1;
        livingTitlePos = -1;
        retrospectPos = -1;
    }

    private void initAdapter(@NonNull IListShowData data, Context context) {
        ArrayList<IListShowData> list = new ArrayList<>();
        list.add(data);
        this.initAdapter(context, list);
    }

    public LiveHomeAdapter getAdapter(Context context) {
        synchronized (syncObj) {
            if (adapter == null) {
                initAdapter(context, null);
            }
        }
        return adapter;
    }

    public IListShowData getAdvanceData() {
        if (advancePos == 0) {
            return adapter.getItem(0);
        } else {
            return null;
        }
    }

    public void setAdvanceData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            advancePos = addOrRefresh(context, 0, advancePos, data);
//            if (livingTitlePos>=0){
//                livingTitlePos++;
//            }
//            if (retrospectPos>=0){
//                retrospectPos++;
//            }
//            freshOtherPos(LiveHomeAdapter.VIEWTYPE_ADVANCE, advancePos, 1);
        }
//        if (adapter == null) {
//            initAdapter(data, context);
//            bannerPos = 0;
//        } else if (bannerPos == -1) {
//            bannerPos = adapter.addData(data, 0);
//        } else {
//            int pos = adapter.setDataInPos(data, bannerPos, false);
//            if (pos >= 0) {
//                bannerPos = pos;
//            }
//        }
    }

    public void removeAdvance() {
        synchronized (syncObj) {
            if (adapter == null || adapter.getItemCount() == 0 || advancePos < 0) {
                return;
            }
            adapter.removeByPostion(0);
        }
    }

    public void removeLiveDatas() {
        synchronized (syncObj) {
            if (adapter == null) {
                return;
            }
            if (advancePos == -1) {
                adapter.clean();
            } else {
                adapter.removeFrom(1);
            }
        }
    }

    public void addOrLiveDatas(Context context, List<IListShowData> datas, boolean isReset) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        synchronized (syncObj) {
            if (adapter == null) {
                initAdapter(context, datas);
            } else if (isReset) {
                if (advancePos < 0) {
                    adapter.setData(datas);
                } else {
                    adapter.resetDatasFrom(advancePos + 1, datas, true);
//                    if (upPos>=adapter.getItemCount()) {
//                        adapter.addDatas(datas);
//                    }else {
//                    }
                }
            } else {
                adapter.addDatas(datas);
            }
        }
    }

    private int addOrRefresh(Context context, int targetPos, int temPos, @NonNull IListShowData data) {
        if (targetPos == -1) {
            return temPos;
        }
        if (adapter == null) {
            initAdapter(data, context);
            return 0;
        } else {
            if (temPos == -1) {
                temPos = adapter.addData(data, targetPos);
            } else {
                int pos = adapter.setDataInPos(data, targetPos, false);
                if (pos >= 0) {
                    temPos = pos;
                }
            }
        }
        return temPos;
    }

    public void removeAdItem() {
        synchronized (syncObj) {
            if (adapter == null || advancePos == -1) {
                return;
            } else {
                adapter.removeByPostion(0);
//                int pos = -1;
//                switch (mode) {
//                    case GreatestAdapter.VIEWTYPE_BANNER:
//                        pos = bannerPos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_EXPRESS:
//                        pos = expressPos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_LIVE:
//                        pos = livePos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_POPULAR:
//                        pos = posularPos;
//                        break;
//                }
//                if (adapter.removeByPostion(pos)) {
//                    freshOtherPos(mode, pos, -1);
//                }
            }
        }
    }


    public void onDestory() {
    }
}
